// Leader/funds/get-fund/get-fund.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        balance: 0,
        ableWithdrawBalance: 0,
        money: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            balance: options.balance,
            ableWithdrawBalance: options.ableWithdrawBalance
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    inputMoney(e){
        console.log("inputMoney is ",e);
        this.setData({
            money: e.detail.value
        })
    },
    getAll(){
        this.setData({
            money: this.data.ableWithdrawBalance
        })
    },
    memberLeaderWithdrawal(){
        console.log("this.data.money is ",this.data.money);
        if(isNaN(this.data.money)){
            return wx.showToast({
                title: "提现金额非法",
                icon: "none"
            })
        }
        if(Number(this.data.money) <= 0){
            return wx.showToast({
                title: "提现金额不能小于0",
                icon: "none"
            })
        }
        console.log("this.data.money is ",this.data.ableWithdrawBalance);
        if(Number(this.data.money) > Number(this.data.ableWithdrawBalance)){
            return wx.showToast({
                title: "提现金额不能大于可提现金额",
                icon: "none"
            })
        }
        wx.showLoading({
            title: "正在加载",
        })

        util.request(api.leader.memberLeaderWithdrawal, {
            withdrawal: Number(this.data.money)
        }, "POST").then(res=>{
            wx.hideLoading();
            wx.showToast({
                title: "提现金额稍后就到",
                icon: "success"
            })
            setTimeout(() => {
                wx.navigateBack({
                    delta: 1
                });
            }, 2000);
        }).catch(err=>{
            wx.hideLoading();
            wx.showToast({
                title: err.message,
                icon: "none"
            })
        })

    }
})
